// packageA/pages/service/index.js
const App = getApp();
const WXAPI = require("../../../api/home");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    search: {
      dMinPrice:'',
      dMaxPrice:'',
      iTotalSellMinCount:'',
      iTotalSellMaxCount:''
    },
    saleList: [
      {
        name: "全部",
        maxValue: "",
        minValue: "",
        active: true,
      },
      {
        name: "100以下",
        maxValue: "99",
        minValue: "0",
      },
      {
        name: "100-500",
        maxValue: "500",
        minValue: "100",
      },
      {
        name: "501-1000",
        maxValue: "1000",
        minValue: "501",
      },
      {
        name: "1000以上",
        maxValue: "",
        minValue: "1000",
      },
    ],
    saleList2: [
      {
        name: "全部",
        maxValue: "",
        minValue: "",
        active: true,
      },
      {
        name: "100以下",
        maxValue: "99",
        minValue: "0",
      },
      {
        name: "100-500",
        maxValue: "500",
        minValue: "100",
      },
      {
        name: "501-1000",
        maxValue: "1000",
        minValue: "501",
      },
      {
        name: "1000以上",
        maxValue: "",
        minValue: "1000",
      },
    ],
    isShow: false,
    isLoading: false,
    listIsOver: false,
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    activeTabName: "0",
    serviceTabList: [],
    optionsList: [],
    pageSize: 20,
    pageNum: 1,
    serviceList: [],
    triggered: false,
    top: 0,
    currentPageNum: 0,
    currentIndex: 0,
    dMinPrice:'',
    dMaxPrice:'',
    iTotalSellMinCount:'',
    iTotalSellMaxCount:''
  },
  setOptions() {
    let list = JSON.parse(JSON.stringify(App.globalData.dictTree));
    let res = list.filter((item) => {
      return item.sKey == "fwlb";
    });
    // 手动添加推荐选项卡
    res[0].Child?.unshift({
      sName: "推荐",
      sKey: "0",
    });
    this.setData({
      optionsList: App.globalData.bannerOptions,
      serviceTabList: res[0].Child,
    });
  },
  changService(e) {
    console.log("e", e);
    this.setData({
      pageNum: 1,
      currentIndex: e.detail.name,
    });
    this.setServiceList(e.detail.name);
  },
  cancel() {
    this.setData({
      isShow: false,
    });
  },
  sure() {
    this.setData({
      isShow: false,
      ['search.dMinPrice']:this.data.dMinPrice,
      ['search.dMaxPrice']:this.data.dMaxPrice,
      ['search.iTotalSellMinCount']:this.data.iTotalSellMinCount,
      ['search.iTotalSellMaxCount']:this.data.iTotalSellMaxCount
    });
    this.setServiceList(this.data.currentIndex)
  },
  selectCurrent(val) {
    let arr = JSON.parse(JSON.stringify(this.data.saleList));
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].name == val.currentTarget.dataset.name) {
        arr[i].active = true;
      } else {
        arr[i].active = false;
      }
    }
    console.log("arr", arr);
    this.setData({
      saleList: arr,
    });
    let res = this.data.saleList.filter((item) => {
      return item.active;
    });
    this.setData({
      iTotalSellMinCount: res[0].minValue,
      iTotalSellMaxCount: res[0].maxValue,
    });
  },
  selectCurrent2(val) {
    let arr = JSON.parse(JSON.stringify(this.data.saleList2));
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].name == val.currentTarget.dataset.name) {
        arr[i].active = true;
      } else {
        arr[i].active = false;
      }
    }
    console.log("arr", arr);
    this.setData({
      saleList2: arr,
    });
    let res = this.data.saleList2.filter((item) => {
      return item.active;
    });
    this.setData({
      dMinPrice: res[0].minValue,
      dMaxPrice: res[0].maxValue,
    });
    console.log(res[0].minValue);
  },
  openFilter() {
    this.setData({
      isShow: true,
    });
  },
  onClose() {
    this.setData({
      isShow: false,
    });
  },
  async setServiceList(index) {
    this.setData({
      listIsOver:false,
      isLoading: true,
    });
    let params = {
      PageIndex: this.data.pageNum,
      PageSize: this.data.pageSize,
      iEnabled: 1,
      iAduitState: 3,
      sCategoryCode: index && index != 0 ? index : "",
      dMinPrice:this.data.search.dMinPrice,
      dMaxPrice:this.data.search.dMaxPrice,
      iTotalSellMinCount:this.data.search.iTotalSellMinCount,
      iTotalSellMaxCount:this.data.search.iTotalSellMaxCount
    };
    let isCustomLoading = true;
    let res = await WXAPI.getMyServiceList(params, isCustomLoading);
    let listArr = [];
    if (res.data.code == 0) {
      if (this.data.pageNum == 1) {
        listArr = res.data.data.list;
        if (listArr && listArr.length > 0) {
          for (let i = 0; i < listArr.length; i++) {
            listArr[i].sFileUrl = this.data.imgBaseUrl + listArr[i].sFileUrl;
          }
        }
      } else {
        listArr = this.data.serviceList;
        for (let i = 0; i < res.data.data.list.length; i++) {
          listArr[i].sFileUrl =
            this.data.imgBaseUrl + res.data.data.list[i].sFileUrl;
        }
        Array.prototype.push.apply(listArr, res.data.data.list);
      }

        this.setData({
          listIsOver: true,
        });
  
      this.setData({
        serviceList: listArr,
        triggered: false,
        currentPageNum: res.data.data.list.length,
        isLoading: false,
      });
    }
  },
  //上拉触底
  onScrollBottom() {
    if (this.data.pageNum < this.data.currentPageNum) {
      this.setData({
        pageNum: this.data.pageNum + 1,
      });
      this.setServiceList(this.data.currentIndex);
    } else if (
      this.data.pageNum > this.data.currentPageNum ||
      this.data.pageNum == this.data.currentPageNum
    ) {
      this.setData({
        listIsOver: true,
      });
    }
  },
  //下拉刷新
  async onRefresh() {
    this.setData({
      pageNum: 1,
      serviceList: [],
    });
    this.setServiceList(this.data.currentIndex);
  },
  onOpenBefore(){
    console.log(this.data.search);
    if (this.data.search.dMinPrice) {
      let arr = JSON.parse(JSON.stringify(this.data.saleList2));
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].minValue == this.data.search.dMinPrice) {
          arr[i].avtive = true;
        } else {
          arr[i].avtive = false;
        }
      }
      this.setData({
        saleList2: arr,
      });
      console.log('arr2',arr);
    } else {
      this.setData({
        saleList2: [
          {
            name: "全部",
            maxValue: "",
            minValue: "",
            active: true,
          },
          {
            name: "100以下",
            maxValue: "99",
            minValue: "0",
          },
          {
            name: "100-500",
            maxValue: "500",
            minValue: "100",
          },
          {
            name: "501-1000",
            maxValue: "1000",
            minValue: "501",
          },
          {
            name: "1000以上",
            maxValue: "",
            minValue: "1000",
          },
        ],
      });
    }

    if (this.data.search.iTotalSellMinCount) {
      let arr = JSON.parse(JSON.stringify(this.data.saleList));
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].minValue == this.data.search.iTotalSellMinCount) {
          arr[i].avtive = true;
        } else {
          arr[i].avtive = false;
        }
      }
      this.setData({
        saleList: arr,
      });
      console.log('arr2',arr);
    } else {
      this.setData({
        saleList: [
          {
            name: "全部",
            maxValue: "",
            minValue: "",
            active: true,
          },
          {
            name: "100以下",
            maxValue: "99",
            minValue: "0",
          },
          {
            name: "100-500",
            maxValue: "500",
            minValue: "100",
          },
          {
            name: "501-1000",
            maxValue: "1000",
            minValue: "501",
          },
          {
            name: "1000以上",
            maxValue: "",
            minValue: "1000",
          },
        ],
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setOptions();
    this.setServiceList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
